package com.itheim.orderservice.service;

import com.alibaba.excel.EasyExcel;
import com.itheim.orderservice.ImportExcelListener;
import com.itheim.orderservice.bean.OrderIdBean;
import com.itheim.orderservice.mapper.OrderExportExcelMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
@Slf4j
public class OrderImportExcelServiceImpl implements OrderImportExcelService {

    @Autowired
    private OrderExportExcelMapper orderExportExcelMapper;

    @Autowired
    private ImportExcelListener importExcelListener;

    /**
     * 普通导入
     *
     * @param file
     */
    @Override
    public void ordinaryImportExcel(MultipartFile file) {

        long beforeTime = System.currentTimeMillis();

        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            log.error("导入报错 " + System.currentTimeMillis() + e.getMessage());
        }
        List<OrderIdBean> objects = EasyExcel.read(inputStream).head(OrderIdBean.class).sheet().doReadSync();
        orderExportExcelMapper.addList(objects);

        long afterTime = System.currentTimeMillis();
        log.info("耗时:{}", afterTime - beforeTime);
    }

    /**
     * 百万级别导入
     *
     * @param file
     */
    @Override
    public void millionExportExcel(MultipartFile file) {
        long beforeTime = System.currentTimeMillis();

        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            log.error("导入报错 " + System.currentTimeMillis() + e.getMessage());
        }
        EasyExcel.read(inputStream,OrderIdBean.class,importExcelListener).doReadAll();

        long afterTime = System.currentTimeMillis();
        log.info("耗时:{}", afterTime - beforeTime);
    }
}
